MEGpipeline_SegmentMRI.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Segments preprocessed MRI via Fieldtrip: %
  3. % Inputs: Resliced MRI data from Fieldtrip. %
  4. % Last modified: Jan. 15, 2014 %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % Copyright (C) 2013-2014, Michael J. Cheung
  7. %
  8. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  9. % details, see the documentation included with the software package.
  10. %
  11. % MEGPLS is free software: you can redistribute it and/or modify it under
  12. % the terms of the GNU General Public License version 2 as published by
  13. % the Free Software Foundation. This program is distributed in the hope
  14. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  15. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. % See the GNU General Public License for more details.
  17. %
  18. % You should have received a copy of the GNU General Public License along
  19. % with this program. If not, you can download the license here:
  20. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  21. function MEGpipeline_SegmentMRI(BuilderMat)
  22. % Make sure java paths added:
  23. UseProgBar = CheckParforJavaPaths;
  24. % Load builder .mat file:
  25. Builder = load(BuilderMat);
  26. name = Builder.name;
  27. paths = Builder.paths;
  28. FTcfg = Builder.FTcfg;
  29. PipeSettings = Builder.PipeSettings;
  30. % Clear existing Errorlog & Diary:
  31. if exist('ErrorLog_SegmentMRI.txt', 'file')
  32. system('rm ErrorLog_SegmentMRI.txt');
  33. end
  34. if exist('Diary_SegmentMRI.txt', 'file')
  35. system('rm Diary_SegmentMRI.txt');
  36. end
  37. diary Diary_SegmentMRI.txt
  38. ErrLog = fopen('ErrorLog_SegmentMRI.txt', 'a');
  39. %=====================================%
  40. % SEGMENT PREPROCESSED FIELDTRIP MRI: %
  41. %=====================================%
  42. for g = 1:length(name.GroupID)
  43. NumSubj = length(name.SubjID{g});
  44. if UseProgBar == 1
  45. ppm = ParforProgMon(['SEGMENTING MRI FILES: ',name.GroupID{g},'. '], NumSubj, 1, 700, 80);
  46. end
  47. parfor s = 1:length(name.SubjID{g})
  48. CheckInput = CheckPipelineMat(paths.MRIdata{g}{s}, 'SegmentMRI');
  49. if CheckInput == 0
  50. continue;
  51. end
  52. CheckSavePerms(paths.SegMRI{g}{s}, 'SegmentMRI');
  53. cfgSegMRI = [];
  54. cfgSegMRI = FTcfg.SegMRI;
  55. cfgSegMRI.inputfile = paths.MRIdata{g}{s};
  56. cfgSegMRI.outputfile = paths.SegMRI{g}{s};
  57. cfgSegMRI.outputfilepresent = 'overwrite';
  58. disp('Segmenting MRI:')
  59. ft_volumesegment(cfgSegMRI)
  60. if UseProgBar == 1 && mod(s, 1) == 0
  61. ppm.increment(); % move up progress bar
  62. end
  63. end % Subj
  64. if UseProgBar == 1
  65. ppm.delete();
  66. end
  67. end % Group
  68. %=========================%
  69. % CHECK FOR OUTPUT FILES: %
  70. %=========================%
  71. for g = 1:length(name.GroupID)
  72. for s = 1:length(name.SubjID{g})
  73. if ~exist(paths.SegMRI{g}{s}, 'file')
  74. fprintf(ErrLog, ['ERROR: Output segmented MRI file missing:'...
  75. '\n %s \n\n'], paths.SegMRI{g}{s});
  76. end
  77. end
  78. end
  79. %=================%
  80. if exist([pwd,'/ErrorLog_SegmentMRI.txt'], 'file')
  81. LogCheck = dir('ErrorLog_SegmentMRI.txt');
  82. if LogCheck.bytes ~= 0 % File not empty
  83. open('ErrorLog_SegmentMRI.txt');
  84. else
  85. delete('ErrorLog_SegmentMRI.txt');
  86. end
  87. end
  88. fclose(ErrLog);
  89. diary off
  90. end